﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeansWsccProject.App_Code;

namespace DeansWsccProject
{
    public partial class collaboration_requests : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("subscribers"))
            {
                Response.Redirect("~/authors-only.aspx");
            }
            
            if (!IsPostBack)
            {
                populateCollaborationRequestsListView();
            }
        }

        protected void btnPostRequest_Click(object sender, EventArgs e)
        {
            if (tbRequest.Text.Trim() != "")
            {
                insertCollaborationRequestIntoCollaborationsTable();
                //insertCollaborationRequestCollaboratorFkidsIntoJunctionTable();
                populateCollaborationRequestsListView();
                tbRequest.Text = ""; // clear collaboration request text box
                tbRequestRequiredPlaceHolder.Visible = false;
            }
            else
            {
                tbRequestRequiredPlaceHolder.Visible = true;
            }
        }

        protected void btnSendCollaboratorRequest_Click(object sender, EventArgs e)
        {
            string postId = ((Button)sender).CommandArgument;
            insertCollaborationRequestCollaboratorFkidsIntoJunctionTable(postId);
            populateCollaborationRequestsListView();
            //var item = (ListViewItem)btn.NamingContainer;
            //int index = item.DisplayIndex;
            //collaborationRequestsListView.
            //int id = (int)collaborationRequestsListView.DataKeys[itemIndex]["ID"];
        }

        private void populateCollaborationRequestsListView()
        {
            functions.fillDataView(collaborationRequestsListView, "SELECT collaborationRequests.ID, collaborationRequests.description, collaborationRequests.dateOfPost, collaborationRequests.originalPoster, profile.FirstName, profile.LastName, profile.profileUserName FROM collaborationRequests, profile WHERE collaborationRequests.originalPoster = profile.profileUserName ORDER BY [collaborationRequests.ID] DESC", null);
        }

        protected void collaborationRequestsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            // Get the item index of the Item
            int itemIndex = ((ListViewDataItem)e.Item).DisplayIndex;
            
            // Extract the key and cast it to its data type.
            DataKey key = ((ListView)sender).DataKeys[itemIndex];

            var rptImage = e.Item.FindControl("collaboratorsRepeater") as Repeater;
            if (rptImage != null)
            {
                functions.fillRepeater(rptImage, "SELECT DISTINCT [profileUserName], [FirstName], [LastName] FROM [userCollaborationsView] WHERE ([ID] = @f1) AND ([state] = 'requestAccepted') ORDER BY [LastName], [FirstName]", key.Value);
                if (functions.getDataFromTable("SELECT DISTINCT [profileUserName] FROM [userCollaborationsView] WHERE ([ID] = @f1) AND ([state] = 'requestAccepted')", key.Value)[0] == "")
                {
                    e.Item.FindControl("collaboratorsRepeater").Visible = false;
                }
            }

            var btnImage = e.Item.FindControl("btnSendCollaboratorRequest") as Button;
            if (btnImage != null)
            {
                string originalposterUserName = functions.getDataFromTable("SELECT [originalPoster] FROM [collaborationRequests] WHERE ([ID] = @f1)", key.Value)[0];
                if (originalposterUserName == User.Identity.Name)
                {
                    e.Item.FindControl("btnSendCollaboratorRequest").Visible = false;
                }

                string userId = functions.getDataFromTable("SELECT [ID] FROM [profile] WHERE ([profileUserName] = @f1)", User.Identity.Name)[0];
                if (functions.getDataFromTable("SELECT [state] FROM [userCollaborationJunction] WHERE ([collaboration_fkid] = @f1) AND ([user_fkid] = " + userId + ")", key.Value)[0] != "")
                {
                    e.Item.FindControl("btnSendCollaboratorRequest").Visible = false;
                    e.Item.FindControl("btnCollaboratorRequestSent").Visible = true;
                }
            }
        }

        private void insertCollaborationRequestIntoCollaborationsTable()
        {
            object[] parameters = { tbRequest.Text, DateTime.Now.Date, User.Identity.Name };
            functions.putDataInTable("INSERT INTO collaborationRequests(description,dateOfPost,originalPoster) VALUES(@f1,@f2,@f3)", parameters);
        }

        private void insertCollaborationRequestCollaboratorFkidsIntoJunctionTable(string postId)
        {
            object[] parameters = { functions.getDataFromTable("SELECT ID FROM profile WHERE profileUserName = @f1", User.Identity.Name)[0], postId, "requestSent" };
            functions.putDataInTable("INSERT INTO userCollaborationJunction(user_fkid,collaboration_fkid,state) VALUES(@f1,@f2,@f3)", parameters);
        }
    }
}